# coding=utf-8
import csv
import re
import time
import requests

cookies = {
    'NTESSTUDYSI': '5fc3db099512b1',
    'EDUWEBDEVICE': '5191fbd9764c55',
    'utm': 'eyJjIjoiIiwiY3QiOIiLCJ0IjoiIn0=|aHR0cHM6Ly93d3cuYmFpZHUuY29tL2xpbms/dXJsPU1BYUdtRVZPaXR3eUJEOWNEZ2dVeHVwVzVoYkxaa01TOFJ2MVoySlA1Sl95TE42akpjYWszQlNwYWhTOFRMUmkmd2Q9JmVxaWQ9ZDRjYjE0ZjcwMDBmNTcyMTAwMDAwMDA2NjIwYmEzYWU=',
    'hb_MA-A976-948FFA05E931_source': 'www.baidu.com',
    'Hm_lvt_77dc9a9d49448cf5e629e5bebaa5500b': '1644929970',
    'WM_NI': 'UPOlREGdjzjOvdIljNeGhiS3mCPBGT4cW4IbQzzotde3bDFjxy0rQyz1DJKKtNtccuCNvYEF2WIUkXVwo%2BcRUqQwEDuS1s6gGgSmDOM4b3gLkQXi6A69IHwWf4Mg7vTGQ3Y%3D',
    'WM_NIKE': '9ca17ae2e6ffcda170e2e6eea98fa2c45e938e9f84f43e8daaa687f033f58bfb89f22af0fea7c3b92a9790818dd14f85bca69be945b6bc878de946a799a88ad239af9da0a3c660f8bd99d5cc6a93a6b690f770a7edaea5c562bca70093d473baef8cabca41b892bc84e27f8192babac84ef28987b6ea5496a9a1cce25a97af9f98c850b78dff94b56d85b99d8ec7458fecaed4e648a98f988ab54bf6e787a9f65da6ec8388b67e98999fd4dc37e2a3',
    'WM_TID': 'l11OZ3UmE7VERERBRFd67eryPj8rBtl5',
    '__yadk_uid': 'Z0EVNBPgekMaQxq94vW1j2Hfu3JeYe1M',
    'STUDY_SESS': 'W82H6rtyXtKzXcbPVN+mSgnaYh0afgf77XWKfX5BfjUJMystTtZlh2Y7miuqQFz4svD4p4lBCKenxDTVFWSm5u2a/NxdGsNnvtMD+Um67M2QXgXEFrzp3Ydje9Z7VHa415127CboE4T17f8LTSI0SfYlIwAwdFoav0Ombg5GHW77XSPPwmlO8byy1OW4QUq/+P6MxCmnJEvne6pPMc9TTJJnThNrM7aj0X5LVpSBvjanPXHxsZaInuRtorF4zFJQ0N3vfK+9vqyaJ+7AEDvoPGgxVMTE1jjOD3rITVYl4npKlOh/Gwx6G1S/X4FQ7qd/thSBd+IpB76/3HWlVfOUZ1dM2nY7woA6U9EQ2JmuHmxg5PRb+6TELB4M6tnfDYXp',
    'STUDY_INFO': 'UID_FEC99F33328D5CDA5687EE563FB47318|4|1143627689|1644929980288',
    'NETEASE_WDA_UID': '1143627689#|#1533199987587',
    'CLIENT_IP': '2231',
    'Hm_lpvt_77dc9a9d49448cf5e629e5bebaa5500b': '1644931741',
}

headers = {
    'Host': 'www.icourse163.org',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Microsoft Edge";v="98"',
    'sec-ch-ua-mobile': '?0',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.50',
    'sec-ch-ua-platform': '"Windows"',
    'content-type': 'text/plain',
    'accept': '*/*',
    'origin': 'https://www.icourse163.org',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-mode': 'cors',
    'sec-fetch-dest': 'empty',
    'referer': 'https://www.icourse163.org/learn/BIT-1001870001?tid=1001962001',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
}
data = {
    "callCount": 1,
    "scriptSessionId": '${scriptSessionId}190',
    "httpSessionId": '5fc3db1',
    "c0-scriptName": 'PostBean',
    "c0-methodName": 'getAllPostsPagination',
    "c0-id": 0,
    "c0-param0": 'number:1001962001',
    "c0-param1": 'string:1002065001',
    "c0-param2": 'number:1',
    "c0-param3": 'string:1',
    "c0-param4": 'number:20',
    "c0-param5": 'boolean:false',
    "c0-param6": 'null:null',
    "batchId": round(time.time() * 1000),
}
            # param3 是控制页数的参数
            # c0-param0 是tid参数
            # c0-param1是讨论区id
            # c0-param4 控制一次返回的量，
            # 剩下的自己挖掘，有不懂的可以联系微信公众号：Twitter账号租赁




def main():
    '''
    title 标题 | shortIntroduction 内容 |anonymous 匿名 | postTime 发表时间 | nickName 昵称 |id s1.id 这样的是帖子id 接下来请求回复有用
    '''
    response = requests.post('https://www.icourse163.org/dwr/call/plaincall/PostBean.getAllPostsPagination.dwr',
                             headers=headers, cookies=cookies, data=data)
    html_re = filter_tags(response.text)
    html_js = reply_change(html_re)  # 掐头去尾最后的数据
    title = re.findall(r'title="(.*?)";', html_js)  # 标题
    content = re.findall(r'shortIntroduction="(.*?)";', html_js)  # 内容
    anonymous = re.findall(r'anonymous=(.*?);', html_js)  # 是否匿名 0为否 1 为是
    replytime = re.findall(r'postTime=(.*?);', html_js)  # 发表时间
    nickName = re.findall(r'nickName="(.*?)";', html_js)  # 昵称
    tieziid = re.findall(r'id=(.*?);s', html_js)
    for t,c,a,r in zip(title,content,anonymous,replytime): #标题 内容 是否匿名 发帖时间
        print('标题:',t)
        if len(c) == 0:
            print('该帖子没有内容.')
        else:
            print('内容:',c)
        if a == '1':
            print('是否匿名 : 是')
        else:
            print('是否匿名 : 否')
        print('发表时间',timeStamp(int(r)))
        print('---')


def Reply_Content(param_Id):
    '''
    获取回复的内容
    param0 就是 在main函数中需要获取的id 然后填入 进行请求得到回复的评论
    param2 是控制评论页数的
    '''
    data_post = {
        'callCount': 1,
        'scriptSessionId': '${scriptSessionId}190',
        'httpSessionId': '6b0a3f5c9d414abaafa9bec696ab7c2a',
        'c0-scriptName': 'PostBean',
        'c0-methodName': 'getPaginationReplys',
        'c0-id': 0,
        'c0-param0': 'number:' + str(param_Id),
        'c0-param1': 'number:2',
        'c0-param2': 'number:1',
        'batchId': round(time.time() * 1000),
    }
    url = 'https://www.icourse163.org/dwr/call/plaincall/PostBean.getPaginationReplys.dwr'
    response = requests.post(url, headers=headers, cookies=cookies, data=data_post).text
    html_re = filter_tags(response)
    html_js = reply_change(html_re)  # 掐头去尾最后的数据
    # print(html_js)


def timeStamp(timeNum):  # 转换时间
    timeStamp = float(timeNum / 1000)
    timeArray = time.localtime(timeStamp)
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    return otherStyleTime


def Storage_Csv(item):  # 存储数据
    # file PATH
    filenamem = r''
    with open(filenamem, 'a+', encoding='utf-8-sig', newline='', errors="ignore")as f:
        f_csv = csv.writer(f)
        f_csv.writerows(item)


def filter_tags(htmlstr):  # 返回文本预处理，去除闲杂信息
    # 去除url
    re_curl = re.compile(r'http://[a-zA-Z0-9.?/&=:]*', re.S)
    re_curls = re.compile(r'https://[a-zA-Z0-9.?/&=:]*', re.S)
    # 先过滤CDATA
    re_cdata = re.compile('//<!\[CDATA\[[^>]*//\]\]>', re.I)  # 匹配CDATA
    re_script = re.compile('<\s*script[^>]*>[^<]*<\s*/\s*script\s*>', re.I)  # Script
    re_style = re.compile('<\s*style[^>]*>[^<]*<\s*/\s*style\s*>', re.I)  # style
    # re_br = re.compile('<br\s*?/?>')  # 处理换行
    re_h = re.compile('</?\w+[^>]*>')  # HTML标签
    re_comment = re.compile('<!--[^>]*-->')  # HTML注释
    s = re_curl.sub('', htmlstr)  # 去除url
    s = re_curls.sub('', s)  # 去除url
    s = re_cdata.sub('', s)  # 去掉CDATA
    s = re_script.sub('', s)  # 去掉SCRIPT
    s = re_style.sub('', s)  # 去掉style
    s = re_h.sub('', s)  # 去掉HTML 标签
    s = re_comment.sub('', s)  # 去掉HTML注释
    return s


def reply_change(text):
    # 转换编码，以及去除头尾不必要信息，仅保留javascript内容
    text = text.encode('utf-8').decode("unicode_escape").splitlines()[2:-2]
    for i in range(0, len(text)):
        text[i] = filter_tags(text[i])
    text = "".join(text)
    return text


if __name__ == "__main__":
    main()
    # Reply_Content('1002065001')
